#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

// 这题只能用动态规划做
class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
		int rows = matrix.size();
		int columns = matrix[0].size();
		vector<vector<int>> dp(rows, vector<int>(columns));

		int maxSide = 0;
		for(int i = 0; i < rows; i++){
			for(int j = 0; j < columns; j++){
				if(matrix[i][j] == '1'){
					if(i == 0 || j == 0){
						dp[i][j] = 1;
					}else{
						dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]),dp[i-1][j-1]) + 1;
					}
					maxSide = max(maxSide, dp[i][j]);
				}
			}
		}
		return maxSide * maxSide;
    }
};

int main(){
	vector<vector<char>> matrix = {
	{'1','0','1','0','0'},
	{'1','0','1','1','1'},
	{'1','1','1','1','1'},
	{'1','0','0','1','0'}
	};
	// vector<vector<char>> matrix = {
	// {'1','0','0'},
	// {'1','0','0'},
	// {'1','1','1'},
	// {'1','0','0'},
	// {'1','0','0'},
	// {'1','0','0'}
	// };
	Solution so;
	int maxSquare = so.maximalSquare(matrix);
	cout << maxSquare << endl;
	return 0;
}